package com.braveo.dict;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

public class DictInitializer {
	private final Activity activity;
	private boolean initialized = false;
	private Handler mDictMessageHandler;

	enum InitError{
		DB_NOT_FOUND
	}	
	
	@SuppressWarnings("serial")
	public static class InitialException extends Exception {
		public final InitError error;
		
		public InitialException(InitError error){
			this.error = error;
		}	
		
		public InitError getError(){
			return error;
		}
	}
	
	public static class DictInitCached {
		private final Activity activity;
		private final DictInitializer initializer;
		
		public DictInitCached(Activity activity, DictInitializer init){
			this.activity = activity;
			this.initializer = init;
		}

		public Activity getActivity() {
			return activity;
		}

		public DictInitializer getInitializer() {
			return initializer;
		}		
	}
	
	
	private DictInitializer(Activity ctx){
		this.activity = ctx;
	}
	
	private static final int CACHE_SIZE = 10;	
	private static final List<DictInitCached> initCaches = new ArrayList<DictInitCached>(CACHE_SIZE);
	
	public static synchronized DictInitializer getInitializer(Activity ctx){
		int size = initCaches.size();
		int k = size - 1;
		for(; k>=0; --k){
			if(initCaches.get(k).getActivity() == ctx){
				break;
			}
		}
		if(k >= 0){
			DictInitCached c = initCaches.remove(k);
			initCaches.add(c);
		}else{
			if(size == CACHE_SIZE){
				initCaches.remove(0);
			}
			initCaches.add(new DictInitCached(ctx, new DictInitializer(ctx)));
		}
		return initCaches.get(initCaches.size() - 1).getInitializer();
	}
	
	public void init() throws InitialException{
		if(initialized)
			return;
		
        TtsManager.getInstance(activity.getApplicationContext());

        List<String> dbList = BraveDictDbAdapter.listDictDbs();
        if(dbList==null || dbList.size()==0){
        	throw new InitialException(InitError.DB_NOT_FOUND);
        }
        

        mDictMessageHandler = new Handler(){
        	public void handleMessage(Message msg){
        		if(msg.what == DictConst.MESSAGE_CHOOSE_DATABASE){
        			Intent x = new Intent(activity, DatabaseChooseView.class);
        			activity.startActivityForResult(x, DictConst.ACTIVITY_CHOOSE_DB);
        		}        		
        	}
        };    
        
        BraveConfigManager configManager = BraveConfigManager.getManager(activity);
        String dictName = configManager.getDictName();
        if(dictName != null){
        	String dbName = BraveDictDbAdapter.dictShortNameToFullName(dictName);
        	try{
        		BraveDictDbAdapter.getInstance(activity).setDbName(dbName);
        	}catch(FileNotFoundException fnfe){
        		mDictMessageHandler.sendEmptyMessage(DictConst.MESSAGE_CHOOSE_DATABASE);
        	}
        }else{
        	mDictMessageHandler.sendEmptyMessage(DictConst.MESSAGE_CHOOSE_DATABASE);
        }  
        
        this.initialized = true;
        return;
	}

	public Handler getDictMessageHandler() {
		return mDictMessageHandler;
	}

	
}
